<?php
class User
{
	public $db_server;
	
	function User($db_hostname)
	{
		//Connects to database
		$db_server = mysql_connect($db_hostname, 'root', '');
		if(!$db_server) 			die("Unable to connect to MySQL: " .	mysql_error() . "<br />
										<a href='index.html'>Return</a>");
										
		$query = "CREATE DATABASE IF NOT EXISTS data";
		mysql_query($query) or 		die("Unable to create database: " . mysql_error() . "<br />
										<a href='index.html'>Return</a>");
										
		mysql_select_db('data') or	die("Unable to connect to database: " . mysql_error() . "<br />
										<a href='index.html'>Return</a>");
										
		$query = "CREATE TABLE IF NOT EXISTS information(Name VARCHAR(32), Number INT(11) UNSIGNED ZEROFILL, Address VARCHAR(64))";													
		mysql_query($query) or 		die("Unable to create table in database: " . mysql_error() . "<br />
										<a href='index.html'>Return</a>");	
	}
	
	function addtodatabase()
	{
		//Checks for duplicate entries
		$query 	= "SELECT Name FROM information";
		$result	= mysql_query($query);
		$rows	= mysql_num_rows($result);
		
		while($row = mysql_fetch_array($result, MYSQL_NUM))
		if($row[0] == $_POST["name"]) die("Unable to add entry to database; name already in use.<br />
											<a href='index.html'>Return</a>");
		
		//Adds data to table
		$query = "INSERT INTO information VALUES('" . $_POST["name"] . "', " . $_POST["number"] . ", '" . $_POST["address"] . "')";
		$result = mysql_query($query);
		if(!$result) die ("Database access failed: " . mysql_error() . "<br />
				<a href='index.html'>Return</a>");
		
		echo	"Successfully added details to database.<br />
				<a href='index.html'>Return</a>";
	}
}
$user = new User('127.0.0.1');
$user->addtodatabase();
?>